<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户id" prop="businessId">
        <el-input
          v-model="queryParams.businessId"
          placeholder="请输入商户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户名" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入商户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户余额" prop="balance">
        <el-input
          v-model="queryParams.balance"
          placeholder="请输入账户余额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="冻结余额" prop="frozenAmount">
        <el-input
          v-model="queryParams.frozenAmount"
          placeholder="请输入冻结余额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可用余额" prop="cashableAmount">
        <el-input
          v-model="queryParams.cashableAmount"
          placeholder="请输入可用余额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需还款" prop="needPayBack">
        <el-input
          v-model="queryParams.needPayBack"
          placeholder="请输入需还款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户安全等级" prop="accountLevel">
        <el-input
          v-model="queryParams.accountLevel"
          placeholder="请输入账户安全等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许重置" prop="canCz">
        <el-input
          v-model="queryParams.canCz"
          placeholder="请输入是否允许重置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许提现" prop="canTx">
        <el-input
          v-model="queryParams.canTx"
          placeholder="请输入是否允许提现"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许透资" prop="canTz">
        <el-input
          v-model="queryParams.canTz"
          placeholder="请输入是否允许透资"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许支付" prop="canZf">
        <el-input
          v-model="queryParams.canZf"
          placeholder="请输入是否允许支付"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许转出" prop="canZc">
        <el-input
          v-model="queryParams.canZc"
          placeholder="请输入是否允许转出"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许转入" prop="canZr">
        <el-input
          v-model="queryParams.canZr"
          placeholder="请输入是否允许转入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否激活状态" prop="accountState">
        <el-input
          v-model="queryParams.accountState"
          placeholder="请输入是否激活状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安全保障等级" prop="securityLevel">
        <el-input
          v-model="queryParams.securityLevel"
          placeholder="请输入安全保障等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易密码" prop="dealPassword">
        <el-input
          v-model="queryParams.dealPassword"
          placeholder="请输入交易密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段1" prop="reserve1">
        <el-input
          v-model="queryParams.reserve1"
          placeholder="请输入预留字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段2" prop="reserve2">
        <el-input
          v-model="queryParams.reserve2"
          placeholder="请输入预留字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段3" prop="reserve3">
        <el-input
          v-model="queryParams.reserve3"
          placeholder="请输入预留字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段4" prop="reserve4">
        <el-input
          v-model="queryParams.reserve4"
          placeholder="请输入预留字段4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段5" prop="reserve5">
        <el-input
          v-model="queryParams.reserve5"
          placeholder="请输入预留字段5"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除标识" prop="delFlg">
        <el-input
          v-model="queryParams.delFlg"
          placeholder="请输入删除标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建用户id" prop="createUserid">
        <el-input
          v-model="queryParams.createUserid"
          placeholder="请输入创建用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新用户id" prop="updateUserid">
        <el-input
          v-model="queryParams.updateUserid"
          placeholder="请输入更新用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:account:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:account:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:account:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:account:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="商户id" align="center" prop="businessId" />
      <el-table-column label="商户名" align="center" prop="businessName" />
      <el-table-column label="账户余额" align="center" prop="balance" />
      <el-table-column label="冻结余额" align="center" prop="frozenAmount" />
      <el-table-column label="可用余额" align="center" prop="cashableAmount" />
      <el-table-column label="需还款" align="center" prop="needPayBack" />
      <el-table-column label="账户安全等级" align="center" prop="accountLevel" />
      <el-table-column label="是否允许重置" align="center" prop="canCz" />
      <el-table-column label="是否允许提现" align="center" prop="canTx" />
      <el-table-column label="是否允许透资" align="center" prop="canTz" />
      <el-table-column label="是否允许支付" align="center" prop="canZf" />
      <el-table-column label="是否允许转出" align="center" prop="canZc" />
      <el-table-column label="是否允许转入" align="center" prop="canZr" />
      <el-table-column label="是否激活状态" align="center" prop="accountState" />
      <el-table-column label="安全保障等级" align="center" prop="securityLevel" />
      <el-table-column label="交易密码" align="center" prop="dealPassword" />
      <el-table-column label="预留字段1" align="center" prop="reserve1" />
      <el-table-column label="预留字段2" align="center" prop="reserve2" />
      <el-table-column label="预留字段3" align="center" prop="reserve3" />
      <el-table-column label="预留字段4" align="center" prop="reserve4" />
      <el-table-column label="预留字段5" align="center" prop="reserve5" />
      <el-table-column label="删除标识" align="center" prop="delFlg" />
      <el-table-column label="创建用户id" align="center" prop="createUserid" />
      <el-table-column label="更新用户id" align="center" prop="updateUserid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:account:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:account:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户id" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商户id" />
        </el-form-item>
        <el-form-item label="商户名" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入商户名" />
        </el-form-item>
        <el-form-item label="账户余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入账户余额" />
        </el-form-item>
        <el-form-item label="冻结余额" prop="frozenAmount">
          <el-input v-model="form.frozenAmount" placeholder="请输入冻结余额" />
        </el-form-item>
        <el-form-item label="可用余额" prop="cashableAmount">
          <el-input v-model="form.cashableAmount" placeholder="请输入可用余额" />
        </el-form-item>
        <el-form-item label="需还款" prop="needPayBack">
          <el-input v-model="form.needPayBack" placeholder="请输入需还款" />
        </el-form-item>
        <el-form-item label="账户安全等级" prop="accountLevel">
          <el-input v-model="form.accountLevel" placeholder="请输入账户安全等级" />
        </el-form-item>
        <el-form-item label="是否允许重置" prop="canCz">
          <el-input v-model="form.canCz" placeholder="请输入是否允许重置" />
        </el-form-item>
        <el-form-item label="是否允许提现" prop="canTx">
          <el-input v-model="form.canTx" placeholder="请输入是否允许提现" />
        </el-form-item>
        <el-form-item label="是否允许透资" prop="canTz">
          <el-input v-model="form.canTz" placeholder="请输入是否允许透资" />
        </el-form-item>
        <el-form-item label="是否允许支付" prop="canZf">
          <el-input v-model="form.canZf" placeholder="请输入是否允许支付" />
        </el-form-item>
        <el-form-item label="是否允许转出" prop="canZc">
          <el-input v-model="form.canZc" placeholder="请输入是否允许转出" />
        </el-form-item>
        <el-form-item label="是否允许转入" prop="canZr">
          <el-input v-model="form.canZr" placeholder="请输入是否允许转入" />
        </el-form-item>
        <el-form-item label="是否激活状态" prop="accountState">
          <el-input v-model="form.accountState" placeholder="请输入是否激活状态" />
        </el-form-item>
        <el-form-item label="安全保障等级" prop="securityLevel">
          <el-input v-model="form.securityLevel" placeholder="请输入安全保障等级" />
        </el-form-item>
        <el-form-item label="交易密码" prop="dealPassword">
          <el-input v-model="form.dealPassword" placeholder="请输入交易密码" />
        </el-form-item>
        <el-form-item label="预留字段1" prop="reserve1">
          <el-input v-model="form.reserve1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留字段2" prop="reserve2">
          <el-input v-model="form.reserve2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="预留字段3" prop="reserve3">
          <el-input v-model="form.reserve3" placeholder="请输入预留字段3" />
        </el-form-item>
        <el-form-item label="预留字段4" prop="reserve4">
          <el-input v-model="form.reserve4" placeholder="请输入预留字段4" />
        </el-form-item>
        <el-form-item label="预留字段5" prop="reserve5">
          <el-input v-model="form.reserve5" placeholder="请输入预留字段5" />
        </el-form-item>
        <el-form-item label="删除标识" prop="delFlg">
          <el-input v-model="form.delFlg" placeholder="请输入删除标识" />
        </el-form-item>
        <el-form-item label="创建用户id" prop="createUserid">
          <el-input v-model="form.createUserid" placeholder="请输入创建用户id" />
        </el-form-item>
        <el-form-item label="更新用户id" prop="updateUserid">
          <el-input v-model="form.updateUserid" placeholder="请输入更新用户id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccount, getAccount, delAccount, addAccount, updateAccount } from "@/api/system/account";

export default {
  name: "Account",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户表格数据
      accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        businessName: null,
        balance: null,
        frozenAmount: null,
        cashableAmount: null,
        needPayBack: null,
        accountLevel: null,
        canCz: null,
        canTx: null,
        canTz: null,
        canZf: null,
        canZc: null,
        canZr: null,
        accountState: null,
        securityLevel: null,
        dealPassword: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        businessId: [
          { required: true, message: "商户id不能为空", trigger: "blur" }
        ],
        businessName: [
          { required: true, message: "商户名不能为空", trigger: "blur" }
        ],
        canCz: [
          { required: true, message: "是否允许重置不能为空", trigger: "blur" }
        ],
        canTx: [
          { required: true, message: "是否允许提现不能为空", trigger: "blur" }
        ],
        canTz: [
          { required: true, message: "是否允许透资不能为空", trigger: "blur" }
        ],
        canZf: [
          { required: true, message: "是否允许支付不能为空", trigger: "blur" }
        ],
        canZc: [
          { required: true, message: "是否允许转出不能为空", trigger: "blur" }
        ],
        canZr: [
          { required: true, message: "是否允许转入不能为空", trigger: "blur" }
        ],
        accountState: [
          { required: true, message: "是否激活状态不能为空", trigger: "blur" }
        ],
        delFlg: [
          { required: true, message: "删除标识不能为空", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户id不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户id不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商户列表 */
    getList() {
      this.loading = true;
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        businessName: null,
        balance: null,
        frozenAmount: null,
        cashableAmount: null,
        needPayBack: null,
        accountLevel: null,
        canCz: null,
        canTx: null,
        canTz: null,
        canZf: null,
        canZc: null,
        canZr: null,
        accountState: null,
        securityLevel: null,
        dealPassword: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAccount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商户编号为"' + ids + '"的数据项？').then(function() {
        return delAccount(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/account/export', {
        ...this.queryParams
      }, `account_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
